<template>
  <div>
    <div class="login-box">
      <img :src="backgrountImg" alt="" />
      <div class="login-bg">
        <div class="login">
          <div>
            <router-link to="/login">
              <div class="login-login">
                <div class="login-after"></div>
                登录
              </div>
            </router-link>
            <router-link to="/sign">
              <div class="login-sign">注册</div>
            </router-link>
            <router-view />
          </div>
          <div>
            <input
              type="text"
              class="username"
              placeholder="用户账号"
              :value="username"
              @change="usernameHandle"
            />
          </div>
          <div>
            <input
              type="password"
              class="username"
              placeholder="用户密码"
              :value="password"
              @change="passwordHandle"
            />
          </div>
          <div style="overflow: hidden">
            <input
              type="text"
              class="verification"
              placeholder="验证码"
              :value="verification"
              @change="verificationHandle"
            />
            <img :src="verificationImg" alt="" class="verification-img" />
          </div>
          <div class="login-button" @click="clickHanle">登录</div>
          <div class="login-hint">你还可以使用第三方登录</div>
          <div class="login-logo" v-for="(item, index) in imgArr" :key="index">
            <a href="#">
              <img :src="item" alt="" />
            </a>
          </div>
          <div class="forget-password">
            <a href="#">忘记密码？</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import { Dialog } from "vant";
export default {
  data() {
    return {
      backgrountImg:
        "http://iwenwiki.com/api/blueberrypai/indexImg/login-back_02.png",
      verificationImg:
        "http://iwenwiki.com/api/blueberrypai/indexImg/check-code_04.png",
      imgArr: [
        "http://iwenwiki.com/api/blueberrypai/indexImg/qqIcon.png",
        "http://iwenwiki.com/api/blueberrypai/indexImg/weixinIcon.png",
        "http://iwenwiki.com/api/blueberrypai/indexImg/weiboIcon.png",
      ],
      username: "",
      password: "",
      verification: "",
    };
  },
  methods: {
    ...mapMutations("login", ["setUser"]),
    clickHanle() {
      this.$api
        .login({
          user_id: this.username,
          password: this.password,
          verification_code: this.verification,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.success) {
            this.setUser({
              username: this.username,
              flag: res.data.success,
            });
            localStorage.setItem(
              "blueberryUser",
              JSON.stringify({
                username: this.username,
                flag: res.data.success,
              })
            );
            this.$router.push("/");
            this.$bus.$emit("currentindex", 1001);
          } else {
            Dialog.alert({
              title: "登录失败!",
              message: res.data.msg,
            }).then(() => {
              // on close
            });
          }
        });
    },
    usernameHandle(e) {
      this.username = e.target.value;
    },
    passwordHandle(e) {
      this.password = e.target.value;
    },
    verificationHandle(e) {
      this.verification = e.target.value;
    },
  },
};
</script>

<style lang="less" scoped>
.login-box {
  img {
    width: 100%;
  }
}
.login {
  width: 340px;
  height: 400px;
  position: absolute;
  top: 200px;
  left: 600px;
  background: rgba(0, 0, 0, 0.3);
  margin: 0 auto;
  .login-login {
    width: 145px;
    height: 40px;
    line-height: 40px;
    background: #fff;
    color: #000;
    margin: 20px 10px 20px 20px;
    float: left;
    cursor: pointer;
    position: relative;
  }
  .login-sign {
    float: left;
    width: 145px;
    line-height: 40px;
    height: 40px;
    background: orange;
    color: #fff;
    margin: 20px 10px 20px 0;
    padding: 0;
    cursor: pointer;
    position: relative;
  }
  .login-after,
  .sign-after {
    width: 0;
    height: 0;
    border-top: 10px solid #fff;
    border-bottom: 0;
    border-left: 10px solid rgba(0, 0, 0, 0);
    border-right: 10px solid rgba(0, 0, 0, 0);
    position: absolute;
    bottom: -10px;
    left: 62.5px;
    z-index: 999;
  }
  .sign-after {
    border-top: 10px solid orange;
  }
  .username {
    width: 295px;
    height: 40px;
    margin-bottom: 10px;
    padding-left: 5px;
  }
  .verification {
    width: 140px;
    height: 40px;
    margin: 0 10px 30px 20px;
    padding-left: 5px;
    float: left;
  }
  .verification-img {
    width: 145px;
    height: 40px;
    margin-bottom: 30px;
    float: left;
  }
  .login-button {
    width: 300px;
    height: 40px;
    background: rgb(111, 163, 33);
    color: #fff;
    margin-left: 20px;
    line-height: 40px;
    margin-bottom: 10px;
    cursor: pointer;
  }
  .login-hint {
    color: #fff;
    font-size: 12px;
    margin-left: 20px;
    margin-bottom: 10px;
    text-align: left;
  }
  .login-logo img {
    width: 30px;
    height: 30px;
    float: left;
    margin-left: 20px;
  }
  .forget-password {
    position: absolute;
    bottom: 20px;
    right: 20px;
    font-size: 12px;
    a {
      color: #fff;
    }
  }
}
</style>